﻿package core.drawing
{
    import core.enum.*;
    import flash.display.*;
    import flash.events.*;

    final public class ColorReplaceTool extends ShapeTool
    {
        var source:BitmapData;
        var sh:Number;
        var ss:Number;
        var sl:Number;
        var th:Number;
        var ts:Number;
        var tl:Number;
        var sldiff:Number;
        var shmin:int;
        var shmax:int;
        var low:int;
        var high:int;
        var px:uint;
        var f:Number;
        var tf:Number;
        var n:int;
        var cx:int;
        var cy:int;
        var sx:int;
        var sy:int;
        var alpha:int;
        var a:int;
        var r:Number;
        var g:Number;
        var b:Number;
        var ta:int;
        var tr:int;
        var tg:int;
        var tb:int;
        var s:Number;
        var h:Number;
        var l:Number;
        var hf:Number;
        var pv:Number;
        var qv:Number;
        var tv:Number;

        public function ColorReplaceTool(param1:Tuyitu)
        {
            super(param1, ToolType.ColorReplace);
            ctrl = ToolType.Picker;
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            if (!type.attitude.locked)
            {
                var _loc_3:int = -1;
                y = -1;
                x = _loc_3;
                this.UpdateReferences();
                this.Prepare(Action.ColorReplaceTool, layer.activeBitmap);
                if (target != null)
                {
                    this.source = layer.bitmap.bitmapData.clone();
                }
                p = canvas.GetOffsetMousePosition(offset);
                this.px = this.source.getPixel32(p.x, p.y);
                this.r = this.px >>> 16 & 255;
                this.g = this.px >>> 8 & 255;
                this.b = this.px & 255;
                this.low = Math.min(this.r, this.g, this.b);
                this.high = Math.max(this.r, this.g, this.b);
                this.sl = this.high / 255;
                _loc_2 = this.high - this.low;
                if (_loc_2)
                {
                    this.ss = _loc_2 / this.high;
                    if (this.r == this.high)
                    {
                        this.sh = (this.g - this.b) / _loc_2 * 60;
                    }
                    else if (this.g == this.high)
                    {
                        this.sh = (2 + (this.b - this.r) / _loc_2) * 60;
                    }
                    else
                    {
                        this.sh = (4 + (this.r - this.g) / _loc_2) * 60;
                    }
                    if (this.sh > 360)
                    {
                        this.sh = this.sh - 360;
                    }
                    else if (this.sh < 0)
                    {
                        this.sh = this.sh + 360;
                    }
                }
                else
                {
                    var _loc_3:int = 0;
                    this.ss = 0;
                    this.sh = _loc_3;
                }
                this.shmax = this.sh + options.tolerance;
                this.shmin = this.sh - options.tolerance;
                if (this.shmax > 360)
                {
                    this.shmax = this.shmax - 360;
                }
                if (this.shmin < 0)
                {
                    this.shmin = this.shmin + 360;
                }
                this.px = ed.toolbar.GetColor(1, layer.editMask);
                this.r = this.px >>> 16 & 255;
                this.g = this.px >>> 8 & 255;
                this.b = this.px & 255;
                this.low = Math.min(this.r, this.g, this.b);
                this.high = Math.max(this.r, this.g, this.b);
                this.tl = this.high / 255;
                _loc_2 = this.high - this.low;
                if (_loc_2)
                {
                    this.ts = _loc_2 / this.high;
                    if (this.r == this.high)
                    {
                        this.th = (this.g - this.b) / _loc_2 * 60;
                    }
                    else if (this.g == this.high)
                    {
                        this.th = (2 + (this.b - this.r) / _loc_2) * 60;
                    }
                    else
                    {
                        this.th = (4 + (this.r - this.g) / _loc_2) * 60;
                    }
                    if (this.th > 360)
                    {
                        this.th = this.th - 360;
                    }
                    else if (this.th < 0)
                    {
                        this.th = this.th + 360;
                    }
                }
                else
                {
                    var _loc_3:int = 0;
                    this.ts = 0;
                    this.th = _loc_3;
                }
                this.sldiff = this.sl - this.tl;
                this.MouseDrag(null);
            }
            return;
        }// end function

        override public function DrawShape(sh:int, sh:int) : void
        {
            var _loc_3:int = 0;
            this.n = 0;
            this.sy = sh - int(shape.height * 0.5);
            this.cy = this.sy + shape.height;
            while (this.sy < this.cy)
            {
                
                this.sx = sh - int(shape.width * 0.5);
                this.cx = this.sx + shape.width;
                while (this.sx < this.cx)
                {
                    
                    this.alpha = shape.data[this.n];
                    if (this.alpha > 0 && (selection == null || selection.IsSelected(this.sx + offset.x, this.sy + offset.y)))
                    {
                        this.px = this.source.getPixel32(this.sx, this.sy);
                        this.a = this.px >>> 24;
                        if (this.a > 0)
                        {
                            this.r = this.px >>> 16 & 255;
                            this.g = this.px >>> 8 & 255;
                            this.b = this.px & 255;
                            this.low = Math.min(this.r, this.g, this.b);
                            this.high = Math.max(this.r, this.g, this.b);
                            this.l = this.high / 255;
                            _loc_3 = this.high - this.low;
                            if (_loc_3)
                            {
                                this.s = _loc_3 / this.high;
                                if (this.r == this.high)
                                {
                                    this.h = (this.g - this.b) / _loc_3 * 60;
                                }
                                else if (this.g == this.high)
                                {
                                    this.h = (2 + (this.b - this.r) / _loc_3) * 60;
                                }
                                else
                                {
                                    this.h = (4 + (this.r - this.g) / _loc_3) * 60;
                                }
                                if (this.h > 360)
                                {
                                    this.h = this.h - 360;
                                }
                                else if (this.h < 0)
                                {
                                    this.h = this.h + 360;
                                }
                            }
                            else
                            {
                                var _loc_4:int = 0;
                                this.s = 0;
                                this.h = _loc_4;
                            }
                            if (this.h < this.shmax && this.h > this.shmin || this.shmax < this.shmin && (this.h > this.shmin || this.h < this.shmax))
                            {
                                this.h = this.th;
                                if (options.matchlight)
                                {
                                    this.f = this.l - 0.5;
                                    if (this.f < 0)
                                    {
                                        this.f = this.f * -1;
                                    }
                                    this.f = (0.5 - this.f) * 2;
                                    this.l = this.l - this.sldiff * this.f;
                                }
                                if (this.l < 0)
                                {
                                    this.l = 0;
                                }
                                else if (this.l > 1)
                                {
                                    this.l = 1;
                                }
                                if (this.l == 0)
                                {
                                    this.r = 0;
                                    this.g = 0;
                                    this.b = 0;
                                }
                                else if (this.s == 0)
                                {
                                    var _loc_4:* = this.l;
                                    this.b = this.l;
                                    var _loc_4:* = _loc_4;
                                    this.g = _loc_4;
                                    this.r = _loc_4;
                                }
                                else
                                {
                                    this.hf = this.h / 60;
                                    this.f = this.hf - int(this.hf);
                                    this.pv = this.l * (1 - this.s);
                                    this.qv = this.l * (1 - this.s * this.f);
                                    this.tv = this.l * (1 - this.s * (1 - this.f));
                                    switch(int(this.hf))
                                    {
                                        case 0:
                                        {
                                            this.r = this.l;
                                            this.g = this.tv;
                                            this.b = this.pv;
                                            break;
                                        }
                                        case 1:
                                        {
                                            this.r = this.qv;
                                            this.g = this.l;
                                            this.b = this.pv;
                                            break;
                                        }
                                        case 2:
                                        {
                                            this.r = this.pv;
                                            this.g = this.l;
                                            this.b = this.tv;
                                            break;
                                        }
                                        case 3:
                                        {
                                            this.r = this.pv;
                                            this.g = this.qv;
                                            this.b = this.l;
                                            break;
                                        }
                                        case 4:
                                        {
                                            this.r = this.tv;
                                            this.g = this.pv;
                                            this.b = this.l;
                                            break;
                                        }
                                        case 5:
                                        {
                                            this.r = this.l;
                                            this.g = this.pv;
                                            this.b = this.qv;
                                            break;
                                        }
                                        case 6:
                                        {
                                            this.r = this.l;
                                            this.g = this.tv;
                                            this.b = this.pv;
                                            break;
                                        }
                                        default:
                                        {
                                            break;
                                        }
                                    }
                                }
                                this.r = this.r * 255;
                                this.g = this.g * 255;
                                this.b = this.b * 255;
                                if (this.alpha < 255)
                                {
                                    this.px = target.getPixel32(this.sx, this.sy);
                                    this.ta = this.px >>> 24 & 255;
                                    this.tr = this.px >>> 16 & 255;
                                    this.tg = this.px >>> 8 & 255;
                                    this.tb = this.px & 255;
                                    this.f = this.alpha / 255;
                                    this.tf = 1 - this.f;
                                    this.tr = this.r * this.f + this.tr * this.tf;
                                    this.tg = this.g * this.f + this.tg * this.tf;
                                    this.tb = this.b * this.f + this.tb * this.tf;
                                    target.setPixel32(this.sx, this.sy, this.ta << 24 | this.tr << 16 | this.tg << 8 | this.tb);
                                }
                                else
                                {
                                    target.setPixel(this.sx, this.sy, this.a << 24 | this.r << 16 | this.g << 8 | this.b);
                                }
                            }
                        }
                    }
                    var _loc_4:String = this;
                    var _loc_5:* = this.n + 1;
                    _loc_4.n = _loc_5;
                    var _loc_4:String = this;
                    var _loc_5:* = this.sx + 1;
                    _loc_4.sx = _loc_5;
                }
                var _loc_4:String = this;
                var _loc_5:* = this.sy + 1;
                _loc_4.sy = _loc_5;
            }
            return;
        }// end function

    }
}
